<?php
namespace app\home\model;

use think\Cache;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;

class Article extends Model
{
    /**
     *最新文章
     * @author wcy
     * @version 2017年7月4日14:44:39
     */
    public   function nowArticle(){
        try{
            $page=input('get.page')==""?"1":input('get.page');
            $key="nowArticle".$page;
            $data=Cache::get($key);
            if(empty($data)){
                $data=Db::table("article")->field("article.id,title,type,type_name,yd_number,dz_number,add_time,introduction,is_yuan")
                    ->where("article.status=1")
                    ->join("type","type.id=article.type","left")
                    ->order("add_time desc")->paginate(10);
                Cache::set($key,$data,10);
                Cache::set($key."f",$data,60*60*24);
            }
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            $data=Cache::get($key."f");
            Cache::set($key,$data,10);
            return $data;
        }
    }
    /**
     *获取文章
     * @author wcy
     * @version 2017年7月4日14:46:33
     */
    public function getArticle($id){
        try{
            $key="getArticle".$id;
            $data=Cache::get($key);
            if(empty($data)){
                $data=Db::table("article")->field("article.*,type.type_name,(SELECT GROUP_CONCAT(label.label_name) from label where  FIND_IN_SET(label.id,article.label)) label_name")
                         ->join("type","type.id=article.type","left")
                        ->where("article.id=$id")->find();
                Cache::set($key,$data,60);
                Cache::set($key."f",$data,60*60*24);
            }
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            $data=Cache::get($key."f");
            Cache::set($key,$data,60);
            return $data;
        }
    }
    /**
     * 热门新闻
     * @author wcy
     * @version 2017年7月4日15:33:39
     */
    public function hotArticle(){
        try{
            $key="hotArticle";
            $data=Cache::get($key);
            if(empty($data)){
                $data=Db::table("article")->field("id,title")->order("dz_number desc")->limit(0,10)->select();
                if(count($data)>0&&$data){
                    Cache::set($key,$data,60);
                    Cache::set($key."f",$data,60*60*24);
                }
            }
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            $data=Cache::get($key."f");
            return $data;
        }
    }
    /**
     * 文章的类别
     * @author wcy
     * @version 2017年7月10日10:24:16
     */
    public function typeArticle($type){
        try{
            $page=input('get.page')==""?"1":input('get.page');
            $key="typeArticle".$type.$page;
            $data=Cache::get($key);
            if(empty($data)){
                $data=Db::table("article")->field("article.id,title,type,type_name,yd_number,dz_number,add_time,introduction,is_yuan")
                    ->where("article.status=1")
                    ->join("type","type.id=article.type","left")
                    ->where("type_name='$type'")
                    ->order("add_time desc")->paginate(10);
                Cache::set($key,$data,60);
                Cache::set($key."f",$data,60*60*24);
            }
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            $data=Cache::get($key."f");
            return $data;
        }
    }
    /**
     * 文章总数
     * @author wcy
     * @version 2017年7月10日15:07:34
     */
    public function conutArticle(){
        try{
            $data=Db::table("article")->count();
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 文章添加阅读数
     * @author wcy
     * @version 2017年7月10日15:34:44
     */
    public function addArticleNumber($id,$number){
        try{
            $res=Db::table("article")->where("id=$id")->setInc("yd_number",$number);
            return $res;
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 模糊查询文章
     * @author wcy
     * @version 2017年7月20日09:35:47
     */
    public function searcehArticle($search){
        try{
            $data=Db::table("article")->field("article.id,title,type,type_name,yd_number,dz_number,add_time,introduction,is_yuan")
                ->where("article.status=1")
                ->join("type","type.id=article.type","left")
                ->where("title like '%$search%'")
                ->order("add_time desc")->paginate(10);
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
    /**
     * 标签查询文章
     * @author wcy
     * @version 2017年7月20日10:02:26
     */
    public function labelArticle($lable){
        try{
            $label_id=Db::table("label")->field("id")->where("label_name='$lable'")->find();
            $id=$label_id["id"];
            $data=Db::table("article")->field("article.id,title,type,type_name,yd_number,dz_number,add_time,introduction,is_yuan")
                ->where("article.status=1 and label like '%$id%'")
                ->join("type","type.id=article.type","left")
                ->order("add_time desc")->paginate(10);
            return $data;
        }catch (Exception $e){
            Log::record($e->getMessage());
            return false;
        }
    }
}